package controllers

import (
	"smart_home/admin/commons"
	"smart_home/admin/models"
)

type EquipmentController struct {
	BaseController
}

// @title 设备列表
// @router /:page [get]
func (c *EquipmentController) Lists() {
	var page int64
	c.Ctx.Input.Bind(&page, ":page")

	if page == 0 {
		page = 1
	}

	lists, count, err := models.GetEquipmentLists(map[string]interface{}{}, page)
	if err != nil {
		c.ErrorResponse(commons.ErrorDBError)
		return
	}

	c.SuccessResponse(map[string]interface{}{
		"count": count,
		"page":  page,
		"list":  lists,
	})
}

// @title 设备信息
// @router /:id [get]
func (c *EquipmentController) Info() {
	var sta string
	c.Ctx.Input.Bind(&sta, ":sta")

	equipment, err := models.GetEquipmentById(sta)
	if err != nil {
		c.ErrorResponse(commons.ErrorDBError)
		return
	}
	if equipment == nil {
		c.ErrorResponse(commons.ErrorEquipmentNoExists)
	}

	c.SuccessResponse(equipment)
}